package com.test.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;

public class PreparedStatmentFetchSize {

	// private final static String JDBC_URL =
	// "jdbc:oracle:thin:SLAM_COLLECT_TUKEDEMO3/SLAM_COLLECT_TUKEDEMO3@beaver.chn.hp.com:1521:SLAMDEV";
	// private final static String SQL =
	// "SELECT INCIDENT_ID,CUST_VISIBLE FROM INCIDENTSM1";

	private final static String JDBC_URL = "jdbc:oracle:thin:slam_bi4/slam_bi4@hslamtl4.chn.hp.com:1521:SLAMST";
	private final static String SQL = "SELECT * FROM A ORDER BY SLA_ID";

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {

			Class.forName("oracle.jdbc.OracleDriver");
			System.out.println("Oracle JDBC driver loaded ok.");

			connection = DriverManager.getConnection(JDBC_URL);
			System.out.println("Connected.");

			int row = 0;

			ps = connection.prepareStatement(SQL);
			ps.setFetchSize(100);
			rs = ps.executeQuery();
			while (rs.next()) {
				int col = 1;
				System.out.println("Times: " + (row++) + ", IncidentId is " + rs.getString(col++));
				if (row % 5 == 0) {
					System.out.println("Sleeping 60 seconds...");
					TimeUnit.SECONDS.sleep(60);
				}
			}
			System.out.println("Total rows " + row);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			System.out.println("Close connection. ");
		}
	}

}
